using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting.YamlDotNet.Core.Tokens;
using UnityEditor;
using UnityEngine;

[CustomPropertyDrawer(typeof(Variable))]
public class VariableDrawer : PropertyDrawer
{
    public SerializedProperty vName;
    public SerializedProperty vType;
    public SerializedProperty vValueInt;
    public SerializedProperty vValueFloat;
    public SerializedProperty vValueBool;
    public SerializedProperty vValueString;
    public SerializedProperty canChange;
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        using (new EditorGUI.PropertyScope(position, label, property))
        {
            vName = property.FindPropertyRelative("vName");
            vType = property.FindPropertyRelative("vType");
            vValueInt = property.FindPropertyRelative("vValueInt");
            vValueFloat = property.FindPropertyRelative("vValueFloat");
            vValueBool = property.FindPropertyRelative("vValueBool");
            vValueString = property.FindPropertyRelative("vValueString");
            canChange = property.FindPropertyRelative("canChange");
            //label宽度
            EditorGUIUtility.labelWidth = 37;
            position.height = EditorGUIUtility.singleLineHeight;

            var vNameRect = new Rect(position)
            {
                width = position.width / 5*1.5f,
                height = position.height,
            };
            var vTypeRect = new Rect(position)
            {
                width = position.width / 5,
                height = position.height,
                x = position.x + position.width / 5*1.5f  + position.width * 0.05f,
            };
            var vValueRect = new Rect(position)
            {
                width = position.width /5*1.5f,
                height = position.height,
                x = position.x + position.width / 5 * 2.5f + position.width*0.1f,
            };
            var canChangeRect = new Rect(position)
            {
                width = position.width / 10,
                height = position.height,
                x = position.x + position.width / 5 * 4 + position.width * 0.14f
            };
            EditorGUI.PropertyField(vNameRect, vName, new GUIContent("Name"), true);
            EditorGUI.PropertyField(vTypeRect, vType, new GUIContent("Type"), true);
            switch (vType.enumValueIndex)
            {
                case (int)VariableType.Int:
                    EditorGUI.PropertyField(vValueRect, vValueInt, new GUIContent("Value"), true);
                    break;
                case (int)VariableType.Float:
                    EditorGUI.PropertyField(vValueRect, vValueFloat, new GUIContent("Value"), true);
                    break;
                case (int)VariableType.Bool:
                    EditorGUI.PropertyField(vValueRect, vValueBool, new GUIContent("Value"), true);
                    break;
                case (int)VariableType.String:
                    EditorGUI.PropertyField(vValueRect, vValueString, new GUIContent("Value"), true);
                    break;
                default:
                    break;
            }
            EditorGUI.PropertyField(canChangeRect, canChange,new GUIContent(""),true);
        }
    }
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        return base.GetPropertyHeight(property, label);
    }
}
